<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 项目管理逻辑
 */
class Project extends AdminBase
{
    
    /**
     * 获取项目列表
     */
    public function getProjectList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelProject->getList($where, $field, $order, $paginate);
    }
     /**
     * 导出项目列表
     */
   
    
    public function exportProjectList($where = [], $field = true, $order = '')
    {
        
        $list = $this->getProjectList($where, $field, $order, false);
        
        $titles = "名称,负责人,项目描述,完成情况,注册时间";
        $keys   = "name,num,describe,degree,create_time";
        
        action_log('导出', '导出项目列表');
        
        export_excel($titles, $keys, $list, '项目列表');
    }
    /**
     * 获取会员列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['project.name'] = ['like', '%'.$data['search_data'].'%'];
        
        if (!is_administrator()) {
            
            $project = session('project_info');
            
            if ($project['is_share_project']) {
                
                $ids = $this->getInheritProjectIds(PROJECT_ID);
                
                $ids[] = PROJECT_ID;
                
                $where['project.id'] = ['in', $ids];
                
            } else {
                
                $where['project.id'] = PROJECT_ID;
            }
        }
        
        return $where;
    }
    /**
     * 项目信息编辑
     */
    public function projectEdit($data = [])
    {
        
        $validate_result = $this->validateProject->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateProject->getError()];
        }
        
        $url = url('projectList');
        
        $result = $this->modelProject->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '项目' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelProject->getError()];
    }

    /**
     * 获取项目信息
     */
    public function getProjectInfo($where = [], $field = true)
    {
        
        return $this->modelProject->getInfo($where, $field);
    }
    
    /**
     * 项目删除
     */
    public function projectDel($where = [])
    {
        
        $result = $this->modelProject->deleteInfo($where);
        
        $result && action_log('删除', '项目删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelProject->getError()];
    }
}
